package com.geeklan.smart.data.repository.pet;

import androidx.annotation.NonNull;

import com.geeklan.smart.data.model.ApiResponse;
import com.geeklan.smart.data.model.CaseInfo;
import com.geeklan.smart.data.model.PetInfo;
import com.geeklan.smart.data.model.RequestFeedPlanDelete;
import com.geeklan.smart.data.model.FeedRule;
import com.geeklan.smart.data.model.UmengMessage;
import com.geeklan.smart.data.model.UpdateFeedRuleByDate;
import com.geeklan.smart.data.remote.RetrofitClient;
import com.geeklan.smart.data.remote.pet.PetApiService;
import com.geeklan.smart.data.repository.RepositoryCallback;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PetRepository {
    private final PetApiService apiService;

    public PetRepository() {
        this.apiService = RetrofitClient.getInstance().getPetApiService();
    }

    public void petList(RepositoryCallback<ApiResponse<List<PetInfo>>> callback) {
        Call<ApiResponse<List<PetInfo>>> call = apiService.petList();
        call.enqueue(new Callback<ApiResponse<List<PetInfo>>>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse<List<PetInfo>>> call, @NonNull Response<ApiResponse<List<PetInfo>>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse<List<PetInfo>>> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }
    public void addPet(PetInfo petInfo,RepositoryCallback<ApiResponse> callback) {
        Call<ApiResponse> call = apiService.addPet(petInfo);
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }
    public void deletePet(PetInfo petInfo,RepositoryCallback<ApiResponse> callback) {
        Call<ApiResponse> call = apiService.deletePet(petInfo);
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }
    public void updatePet(PetInfo petInfo,RepositoryCallback<ApiResponse> callback) {
        Call<ApiResponse> call = apiService.updatePet(petInfo);
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void getFeedRuleList(String deviceId,int repetitionPeriod,String date, RepositoryCallback<ApiResponse<List<FeedRule>>> callback) {
        Call<ApiResponse<List<FeedRule>>> call = apiService.getFeedRuleList(deviceId,repetitionPeriod,date);
        call.enqueue(new Callback<ApiResponse<List<FeedRule>>>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse<List<FeedRule>>> call, @NonNull Response<ApiResponse<List<FeedRule>>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse<List<FeedRule>>> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }


    public void getFeedRuleList(String deviceId, RepositoryCallback<ApiResponse<List<FeedRule>>> callback) {
        Call<ApiResponse<List<FeedRule>>> call = apiService.getFeedRuleList(deviceId);
        call.enqueue(new Callback<ApiResponse<List<FeedRule>>>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse<List<FeedRule>>> call, @NonNull Response<ApiResponse<List<FeedRule>>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse<List<FeedRule>>> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }


    public void updateFeedRuleByDate(UpdateFeedRuleByDate updateFeedRuleByDate, RepositoryCallback<ApiResponse> callback) {
        Call<ApiResponse> call = apiService.updateFeedRuleByDate(updateFeedRuleByDate);
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }


    public void confirmTimeFeed(UmengMessage umengMessage, RepositoryCallback<ApiResponse> callback) {
        Call<ApiResponse> call = apiService.confirmTimeFeed(umengMessage);
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void deleteFeedRule(RequestFeedPlanDelete requestFeedPlanDelete, RepositoryCallback<ApiResponse> callback) {
        Call<ApiResponse> call = apiService.deleteFeedRule(requestFeedPlanDelete);
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void updateFeedRule(FeedRule feedRule, RepositoryCallback<ApiResponse> callback) {
        Call<ApiResponse> call = apiService.updateFeedRule(feedRule);
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void addFeedRule(FeedRule feedRule, RepositoryCallback<ApiResponse> callback) {
        Call<ApiResponse> call = apiService.addFeedRule(feedRule);
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void addPathology(CaseInfo caseInfo, RepositoryCallback<ApiResponse> callback) {
        Call<ApiResponse> call = apiService.addPathology(caseInfo);
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void deletePathology(CaseInfo caseInfo, RepositoryCallback<ApiResponse> callback) {
        Call<ApiResponse> call = apiService.deletePathology(caseInfo);
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void updatePathology(CaseInfo caseInfo, RepositoryCallback<ApiResponse> callback) {
        Call<ApiResponse> call = apiService.updatePathology(caseInfo);
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void pathologyList(int petId,int limit,int page, RepositoryCallback<ApiResponse<List<CaseInfo>>> callback) {
        Call<ApiResponse<List<CaseInfo>>> call = apiService.pathologyList(petId,limit,page);
        call.enqueue(new Callback<ApiResponse<List<CaseInfo>>>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse<List<CaseInfo>>> call, @NonNull Response<ApiResponse<List<CaseInfo>>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse<List<CaseInfo>>> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

}
